/* 试卷管理 */
import React, { PureComponent } from 'react'
import { Table, Pagination, Form, Button, Select, Input, Modal, message } from 'antd';
import { ExamSubjectCascader, HardLevelSelect } from '../Components/AntComponents';
import { getPaperList, deletePaper } from '@/services/paperInfo'
import { history } from 'umi'
import Authorized from '@/components/Authorized/Authorized'
export default class Index extends PureComponent<any, any> {
    constructor(props: any) {
        super(props);
        this.state = {
            subject: '',
            complexity: '0',
            test_type: '0',
            name: '',
            data: [],
            count: 1,
            selectedRowKeys: [],
            currentPage: 1
        }
    }
    renderList = (page: string) => {
        const { subject,
            complexity,
            test_type,
            name } = this.state;
        getPaperList({
            page,
            subject: subject[0] ? subject[subject.length - 1] : '',
            complexity,
            test_type,
            name
        }).then(res => {
            this.setState({
                data: res.data,
                count: res.count
            })
        })
    }
    changeSubject = (e: any) => {
        this.setState({
            subject: e
        })
    }
    changeComplexity = (e: any) => {
        this.setState({
            complexity: e
        })
    }
    changeTestType = (e: any) => {
        this.setState({
            test_type: e
        })
    }
    changeName = (e: any) => {
        this.setState({
            name: e.target.value
        })
    }
    componentDidMount() {
        this.renderList('1')
    }
    goEdit = (id: string, row: any) => () => {
        if (row.type === 1) {
            if (location.href.includes('tzExamInfo')) {
                history.push('/editTzRandom/' + id)
            } else {
                history.push('/editRandom/' + id)
            }
        }
        if (row.type === 3) {
            if (location.href.includes('tzExamInfo')) {
                history.push('/editTzManual/' + id)
            } else {
                history.push('/editManual/' + id)
            }
        }
    }
    goShow = (id: string, row: any) => () => {
        if (row.type === 1) {
            if (location.href.includes('tzExamInfo')) {
                history.push('/showTzRandom/' + id)
            } else {
                history.push('/showRandom/' + id)
            }
        }
        if (row.type === 3) {
            if (location.href.includes('tzExamInfo')) {
                history.push('/showTzManual/' + id)
            } else {
                history.push('/showManual/' + id)
            }
        }
    }
    goPrintPaper = (id: string, row: any) => () => {
        history.push('/printPaper/' + id + '/' + row.type)
    }
    goPrintAnswer = (id: string, row: any) => () => {
        history.push('/printAnswer/' + id + '/' + row.type)
    }
    changePage = (e: number) => {
        this.renderList(e + '')
        this.setState({
            currentPage: e
        })
    }
    searchHandle = () => {
        this.renderList('1')
        this.setState({
            currentPage: 1
        })
    }
    goAnalyze = (id: any) => () => {
        if (location.href.includes('tzExamInfo')) {
            history.push('/paperTzAnalyze/' + id)
        } else {
            history.push('/paperAnalyze/' + id)
        }
    }
    deleteHandle = (id: any) => () => {
        const { selectedRowKeys } = this.state;
        const ids = id ? [id] : selectedRowKeys;
        Modal.confirm({
            title: '提示',
            content: '确定删除试卷？',
            onOk: () => {
                deletePaper({ id: ids }).then(res => {
                    if (res.status === 1) {
                        message.success('已删除')
                        this.renderList('1');
                        this.setState({
                            selectedRowKeys: []
                        })
                    } else {
                        message.error('删除失败')
                    }
                })
            }
        })
    }
    render() {
        const { subject, complexity, data, count, selectedRowKeys, currentPage } = this.state;
        const columns = [
            {
                title: '考试名称',
                dataIndex: 'name'
            },
            {
                title: '所属科目',
                dataIndex: 'subject',
                render: (subject) => {
                    return <p style={{ width: 250 }}>{subject}</p>
                }
            },
            {
                title: '组卷模式',
                dataIndex: 'type',
                render: (type: number) => {
                    return type === 1 ? '随机组卷' : '人工组卷'
                }
            },
            {
                title: '总分',
                dataIndex: 'total'
            },
            {
                title: '时长',
                dataIndex: 'test_time'
            },
            {
                title: '类型',
                dataIndex: 'test_type',
                render: (test_type: number) => {
                    return test_type === 1 ? '正式考试' : '模拟考试'
                }
            },
            // {
            //     title: '试卷信息',
            //     dataIndex: 'id',
            //     render: (id: string, row: any) => {
            //         return <React.Fragment>
            //             <a onClick={this.goPrintPaper(id, row)} style={{ marginRight: 20 }}>试卷</a>
            //             <a onClick={this.goPrintAnswer(id, row)}>答案</a>
            //         </React.Fragment>
            //     }
            // },
            {
                title: '操作',
                dataIndex: 'id',
                render: (id: string, row: any) => {
                    return <React.Fragment>
                        <Authorized authority={[503, 99999993]}>
                            <a onClick={this.goEdit(id, row)} style={{ marginRight: 20 }}>修改</a>
                        </Authorized>
                        <Authorized authority={[504, 99999994]}>
                            <a onClick={this.deleteHandle(id)} style={{ marginRight: 20 }}>删除</a>
                        </Authorized>
                        {/* <a onClick={this.goShow(id, row)}>查看</a> */}
                        <a onClick={this.goAnalyze(id)} style={{ marginLeft: 20 }}>统计</a>

                    </React.Fragment>
                }
            },

        ]
        const rowSelection = {
            selectedRowKeys,
            onChange: (selectedRowKeys: any) => {
                this.setState({
                    selectedRowKeys
                })
            },
        };

        return (
            <div>
                <div style={{ overflow: 'hidden', marginBottom: 20 }}>
                    <Form style={{ float: 'right' }} layout="inline">

                        <Form.Item>
                            <ExamSubjectCascader onChange={this.changeSubject} value={subject} />
                        </Form.Item>
                        <Form.Item>
                            <Select defaultValue="0" onChange={this.changeTestType}>
                                <Select.Option key="0" value="0">所有试卷</Select.Option>
                                <Select.Option key="2" value="2">模拟考试</Select.Option>
                                <Select.Option key="1" value="1">正式考试</Select.Option>
                            </Select>
                        </Form.Item>
                        <Form.Item>
                            <HardLevelSelect onChange={this.changeComplexity} value={complexity} />
                        </Form.Item>
                        <Form.Item>
                            <Input placeholder="搜索考试名称" onChange={this.changeName} />
                        </Form.Item>
                        <Form.Item>
                            <Button type="primary" onClick={this.searchHandle}>搜索</Button>
                        </Form.Item>
                    </Form>
                </div>
                <Table scroll={{ x: 1400 }} rowSelection={rowSelection} rowKey="id" dataSource={data} pagination={false} columns={columns}
                    footer={() => <Authorized authority={[504, 99999994]}><Button danger type="primary" onClick={this.deleteHandle('')}>删除所选</Button></Authorized>}
                ></Table>
                <Pagination
                    showQuickJumper
                    locale="zh-CN"
                    onChange={this.changePage}
                    current={currentPage}
                    total={count}
                    style={{ float: 'right', marginTop: 25 }}
                    showTotal={(total, range) => `第${range[0]}条-第${range[1]}条，共 ${total} 条`}
                />
            </div>
        )
    }
}
